<?php
namespace Admin\Common\Model;
use Think\Model\RelationModel;
class CommonModel extends RelationModel
{
    //获取一条
    public function getOne($where = array())
    {
        $result = $this->where($where)->find();
        return $result;
    }

    //获取列表
    public function getList($where = array(), $order = array('id'=>"DESC"))
    {
        //开始分页信息
        $count = $this->count();
        //分页
        $Page = new \Think\Page($count, 10);//实例化分页类 传入总记录数和每页显示的记录数(25)
        $show = $Page->show();  //分页显示输出
        $limit = $Page->firstRow . ',' . $Page->listRows;
        $list = $this->where($where)->order($order)->limit($limit)->select();
        for ($i=0; $i < count($list); $i++) { 
           $sysdata = M('Sysuser')->where('userid="'.$list[$i]['userid'].'"')->find();
           $list[$i]['true_name'] = $sysdata['username'];
        }
        $arr = array('count' => $count, 'list' => $list, 'show' => $show, 'nowPage' => $Page->nowPage, 'totalPages' => $Page->totalPages);
        return $arr;
    }

    //获取所有
    public function getAll($where = array(), $order = array('id'=>"DESC"), $limit = '')
    {
        $list = $this->where($where)->order($order)->limit($limit)->select();
        return $list;
    }

    //计算总数
    public function countSum($where = array())
    {
        $arr = $this->where($where)->count();
        return $arr;
    }

    //添加
    public function insert($map = array())
    {
        $id = $this->add($map);
        return $id;
    }

    //修改
    public function edit($map = array())
    {
        $result = $this->save($map);
        return $result;
    }

    //删除
    public function del($where = array())
    {
        $result = $this->where($where)->delete();
        return $result;
    }
    //减法
    public function dec($where = array(),$colum="",$num=""){
        $this->where($where)->setDec($colum,$num);
    }
}